/**
 * Malsha 2014-11-12
 */

IssuerMapping = new Object();

var unmapped_issuer_store = Ext.create('Ext.data.Store', {
	fields : [ 'calIssuerCode', 'cseIsuCode', 'cseIsuName', 'issuer' ]
});

IssuerMapping.getCenterPanel = function() {

	Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		} ],
		storeId : 'calIssuerStore',
	});

	Ext.Ajax.request({
		url : 'getUnmapingIssuers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			Ext.data.StoreManager.lookup('calIssuerStore').loadRawData(obj.issuerDtos);
		},
		failure : function(response) {
			Ext.Msg.alert('Error!', 'Failed to Load Issuers!');
		}

	});
	grid = Ext.create('Ext.grid.Panel', {
		store : unmapped_issuer_store,
		height : 380,
		plugins : [ Ext.create('Ext.grid.plugin.CellEditing', {
			clicksToEdit : 1
		}) ],
		columns : [ {
			header : 'CSE CODE',
			dataIndex : 'cseIsuCode',
			width : 100
		}, {
			header : 'CSE ISSUER NAME',
			dataIndex : 'cseIsuName',
			width : 200,
			flex : 1
		}, {
			header : 'CALI CODE',
			dataIndex : 'calIssuerCode',
			width : 200,
			flex : 1,
			editor : {
				xtype : 'combo',
				store : Ext.data.StoreManager.lookup('calIssuerStore'),
				valueField : 'code',
				queryMode : 'local',
				allowBlank : false,
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - {name}<br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {name}', '</tpl>')
			}
		} ],

	});

	grid.on('edit', function(editor, e) {

		if (e.field == 'calIssuerCode') {
			var selectcode = e.record.data.calIssuerCode;

			var recordIndex = unmapped_issuer_store.findBy(function(record, id) {

				if (id != e.record.data.id) {
					if (record.get('calIssuerCode') === selectcode) {
						return true;
					}
					return false;
				}
			});

			if (recordIndex != -1) {
				Ext.Msg.alert('Error', "Duplicate Matching");
				e.record.data.calIssuerCode = "";
				e.record.commit();
			}
		}

	});

	return formpanel = Ext.create('Ext.form.Panel', {
		title : 'Issuer Mapping With CSE File',
		bodyPadding : 20,
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 110,
			anchor : '75%',
		},
		items : [ grid ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					var mappings = [];
					unmapped_issuer_store.each(function(record) {
						if (record.get('calIssuerCode') != null) {
							var obj = {
								"cseIsuCode" : record.get("cseIsuCode"),
								"cseIsuName" : record.get("cseIsuName"),
								"calIssuerCode" : record.get("calIssuerCode")
							};
							mappings.push(obj);
						}
					});
					form.url = 'saveissuermapping.action';
					form.submit({
						params : {
							"mappings" : Ext.encode(mappings),
						},
						success : function(form, action) {
							form.reset();
							Ext.getCmp('save').setText('Save');
							Ext.Msg.alert('Success', 'Saved Successfully');
							Ext.Ajax.request({
								url : 'getNotMappedIsser.action',
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									unmapped_issuer_store.loadRawData(obj.codeMappingDtos);
								}
							});
							Ext.Ajax.request({
								url : 'getUnmapingIssuers.action',
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.data.StoreManager.lookup('calIssuerStore').loadRawData(obj.issuerDtos);
								},
								failure : function(response) {
									Ext.Msg.alert('Error!', 'Failed to Load Issuers!');
								}

							});
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', action.result.resulteMsg);
						}
					});
				}
			}
		} ],
		listeners : {
			render : function(p) {
				Ext.Ajax.request({
					url : 'getNotMappedIsser.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						unmapped_issuer_store.loadRawData(obj.codeMappingDtos);
					}
				});
			}
		}
	});
};